import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { BusinessFile } from "@/api/backend/index";
import { ElNotification } from "element-plus"; //处理导航信息

export const useLedgerFile = defineStore(Names.ledgerFile, {
  state: () => {
    return {
      orderId: 0,
      opera: "add",
      pageNum: 1,
      pageSize: 10,
      single: {
        id: 0,
        orderId: 0,
        fileName: "",
        fileUrl: "",
        isNecessary: "",
        companyId: 0,
        companyName: "",
      },
    };
  },
  getters: {},
  actions: {
    //获取部门下拉框列表信息
    // getDept() {
    //   return new Promise((resolve, reject) => {
    //     _get(sysMDept + "/treeselect", {}, "json", "")
    //       .then((res) => {
    //         console.log(res);
    //         resolve(res);
    //       })
    //       .catch((err) => {
    //         console.log(err);
    //         reject(err);
    //       });
    //   });
    // },
    //编辑文件
    editFile(data: any) {
      return new Promise((resolve, reject) => {
        _put(BusinessFile, data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //新增文件
    addFile(data: any) {
      return new Promise((resolve, reject) => {
        _post(BusinessFile, data, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //删除文件
    deleteFile(data: any) {
      return new Promise((resolve, reject) => {
        _delete(BusinessFile + `/${data.id}`, {}, "json", "")
          .then((res) => {
            console.log(res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //获取文件
    geFileList() {
      return new Promise((resolve, reject) => {
        _get(
          BusinessFile + "/list",
          {
            pageSize: this.pageSize,
            pageNum: this.pageNum,
            orderId: this.orderId,
            companyId: this.single.companyId,
          },
          "json",
          ""
        ).then((res: any) => {
          console.log("获取文件", res);
          resolve(res);
        });
      });
    },
  },
});
